/**
 * 
 */
package org.hotpotmaterial.codegenn2.service.impl;

import java.util.Set;

import javax.sql.DataSource;

import org.hotpotmaterial.codegenn2.dto.DataSourceDTO;
import org.hotpotmaterial.codegenn2.service.IReloadDatasource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;

/**
 * @author cakydeveloper
 *
 */
@Service
public class ReloadDatasourceImpl implements IReloadDatasource {
	
	@Autowired
	private DynamicRoutingDataSource dataSource;
	
	@Autowired
	private DataSourceCreator dataSourceCreator;
	
	@Override
	public Set<String> getAllDatasourcesName() {
		return dataSource.getCurrentDataSources().keySet();
	}

	@Override
	public Set<String> addDatasource(DataSourceDTO dsDTO) {
		DataSourceProperty dataSourceProperty = new DataSourceProperty();
		BeanUtils.copyProperties(dsDTO, dataSourceProperty);
		dataSourceProperty.setType(com.alibaba.druid.pool.DruidDataSource.class);
		DataSource adddataSource = dataSourceCreator.createDataSource(dataSourceProperty);
		dataSource.addDataSource(dsDTO.getPollName(), adddataSource);
		return dataSource.getCurrentDataSources().keySet();
	}

	@Override
	public void removeDatasource(String name) {
		dataSource.removeDataSource(name);
	}

}
